package com.deyou.fragment;

import android.app.DialogFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.deyou.R;
import com.deyou.activity.HouseCalenderSettingActivity;
import com.deyou.model.HouseResourceDetailBean;
import com.deyou.util.DateUtil;
import com.deyou.widget.DateRangeDecorator;
import com.deyou.widget.DateRentStatusDecorator;
import com.squareup.timessquare.CalendarCellDecorator;
import com.squareup.timessquare.CalendarPickerView;
import com.squareup.timessquare.DefaultDayViewAdapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


public class HouseBookInfoFragment extends DialogFragment {

    @BindView(R.id.house_rent_calendar)
    CalendarPickerView houseRentCalendar;
    @BindView(R.id.tv_close_calendar)
    TextView tvCloseCalendar;
    Unbinder unbinder;
    private List<HouseResourceDetailBean.HouseresourcePriceBean> priceDateList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.fragment_house_book_info, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    public static HouseBookInfoFragment newInstance(List<HouseResourceDetailBean.HouseresourcePriceBean> priceDateList) {
        HouseBookInfoFragment fragment = new HouseBookInfoFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("price_date", (Serializable) priceDateList);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        priceDateList = (List<HouseResourceDetailBean.HouseresourcePriceBean>) getArguments().getSerializable("price_date");
        initCalendarView();
    }

    private void initCalendarView() {
        Calendar nextYear = Calendar.getInstance();
        nextYear.add(Calendar.YEAR, 1);
        ArrayList<Date> selectDates = new ArrayList<>();
        for (HouseResourceDetailBean.HouseresourcePriceBean priceBean : priceDateList){
            if (priceBean.isRent == 1){
                selectDates.add(DateUtil.string2Date10(priceBean.priceDate));
            }
        }
        houseRentCalendar.setCustomDayView(new DefaultDayViewAdapter());
        houseRentCalendar.setDecorators(Arrays.<CalendarCellDecorator>asList(new DateRentStatusDecorator(getActivity(), priceDateList)));
        houseRentCalendar.init(new Date(), nextYear.getTime()) //
                .withSelectedDates(selectDates)
                .inMode(CalendarPickerView.SelectionMode.SINGLE)
                .displayOnly();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.tv_close_calendar)
    public void onViewClicked() {
        dismiss();
    }
}
